package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//登录跳转
//@WebServlet("/Servlet01")
public class Servlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
       System.out.println(username+"  "+password);
       Object s = servletContext.getAttribute("s");
        //Object s = req.getAttribute("s");
        //System.out.println(s);
        if (s!=null&&(s.equals("5"))){
            PrintWriter writer = resp.getWriter();
            writer.print("遭受恶意攻击，系统锁定，请执行解锁程序");
        }else {
            if (username != null && password != null && username.equals("root") && password.equals("root")) {
                //System.out.println("登录成功");
                servletContext.setAttribute("key", "lock");
                PrintWriter writer = resp.getWriter();
                writer.print("登录成功");
            }else {
                PrintWriter writer = resp.getWriter();
                writer.print("登录失败");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
